<template>
  <div class="login-container">
    <el-card class="form-card">
    <h2>心血管疾病患病评估</h2>
    <el-form ref="loginForm" :model="formData" @submit.prevent="submitForm">
      <el-form-item label="用户名或邮箱" prop="username_or_email" :rules="{ required: true, message: '请输入用户名或邮箱', trigger: 'blur' }">
        <el-input v-model="formData.username_or_email" placeholder="请输入用户名或邮箱"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password" :rules="{ required: true, message: '请输入密码', trigger: 'blur' }">
        <el-input type="password" v-model="formData.password" placeholder="请输入密码" show-password></el-input>
      </el-form-item>
      <el-button type="primary" native-type="submit">登录</el-button>
      <el-button @click="openRegisterDialog">注册</el-button>
    </el-form>

    <!-- Register Dialog -->
    <el-dialog title="注册" v-model="showRegisterDialog" width="30%" @close="resetRegisterForm">
      <el-form ref="registerForm" :model="registerData" @submit.prevent="submitRegisterForm">
        <el-form-item label="用户名" prop="username" :rules="{ required: true, message: '请输入用户名', trigger: 'blur' }">
          <el-input v-model="registerData.username" placeholder="请输入用户名"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password" :rules="{ required: true, message: '请输入密码', trigger: 'blur' }">
          <el-input type="password" v-model="registerData.password" placeholder="请输入密码" show-password></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email" :rules="{ required: true, message: '请输入邮箱', trigger: 'blur' }">
          <el-input type="email" v-model="registerData.email" placeholder="请输入邮箱"></el-input>
        </el-form-item>
        <div>
          <el-button type="primary" native-type="submit">注册</el-button>
          <el-button @click="showRegisterDialog = false">取消</el-button>
        </div>
      </el-form>
    </el-dialog>
    </el-card>
  </div>
</template>

<script setup>
import { ref, getCurrentInstance} from 'vue';
import axios from 'axios';
import { useRoute, useRouter } from 'vue-router'

const router = useRouter()

const formData = ref({
  username_or_email: '',
  password: ''
});
const registerData = ref({
  username: '',
  password: '',
  email: ''
});


const { proxy } = getCurrentInstance();
const showRegisterDialog = ref(false)


const submitForm = () => {
  axios.post('http://localhost:5000/api/login', formData.value,{
    withCredentials: true,
    headers: {
      'Content-Type': 'application/json'
    }
  })
      .then(response => {
        const { message, status ,username} = response.data;
        proxy.$message({
          message: message,
          type: status === 'success' ? 'success' : 'error',
          duration: 5000
        });

        if (response.status === 200) {
          localStorage.setItem('username', username)
          router.push('/predict')
          console.log('欢迎登录',username)
          console.log('登录成功');
        }
      })
      .catch(error => {
        console.error('请求失败：', error);
        if (error.response) {
          proxy.$message({
            message: error.response.data.message || '请求失败',
            type: 'error',
            duration: 5000
          });
        } else {
          proxy.$message({
            message: '网络错误或服务器未响应',
            type: 'error',
            duration: 5000
          });
        }
      });
};

const openRegisterDialog = () => {
  console.log("Opening register dialog");
  showRegisterDialog.value = true;
};

const submitRegisterForm = () => {
  axios.post('http://localhost:5000/api/register', registerData.value, {
    headers: {
      'Content-Type': 'application/json'
    }
  }).then(response => {
    proxy.$message({
      message: response.data.message,
      type: 'success',
      duration: 5000
    });
    showRegisterDialog.value = false;
    resetRegisterForm();
  }).catch(error => {
    proxy.$message({
      message: error.response.data.message || '注册失败',
      type: 'error',
      duration: 5000
    });
  });
};


const resetRegisterForm = () => {
  registerData.value = { username: '', password: '', email: '' };
};

</script>

<style scoped>

.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background-color: #f8cacc;
}

.form-card {
  padding: 20px;
  background-color: white;
  border-radius: 10px;
  box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
  width: auto;
}

.el-form-item {
  margin-bottom: 20px;
}

.el-input {
  width: 240px;
}
</style>
